package com.hpe.fastfood.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hpe.fastfood.mapper.UserMapper;
import com.hpe.fastfood.pojo.Bussiness;
import com.hpe.fastfood.pojo.Comment;
import com.hpe.fastfood.pojo.User;
import com.hpe.fastfood.service.UserService;
import com.hpe.fastfood.util.Page;
import com.hpe.fastfood.util.ToolUtil;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public Page selectUser(Page page, String userName) throws RuntimeException {
		// TODO Auto-generated method stub
		int start = (page.getCurPage() - 1) * page.getPageNumber();// 起始页
		List<Bussiness> course = userMapper.selectUser(start, page.getPageNumber(), userName);// 分页查询记录
		// 查询一共多少条记录
		int rows = userMapper.selectCount(userName);
		// 计算一共多少页
		int totalPage = rows % page.getPageNumber() == 0 ? rows / page.getPageNumber()
				: rows / page.getPageNumber() + 1;
		// 封装数据到page进行放回
		page.setDate(course);
		page.setRows(rows);
		page.setTotalPage(totalPage);
		return page;
	}

	@Override
	public User selectUserMegById(int userId) throws RuntimeException {
		// TODO Auto-generated method stub
		return userMapper.selectUserMegById(userId);
	}

	@Override
	public int updateUserMag(User user) throws RuntimeException {
		// TODO Auto-generated method stub
		return userMapper.updateUserMag(user);
	}

	@Override
	public Page selectCommentByUserId(Page page, int userId) throws RuntimeException {
		// TODO Auto-generated method stub
		int start = (page.getCurPage() - 1) * page.getPageNumber();// 起始页
		List<Comment> comment = userMapper.selectCommentByUserId(start, page.getPageNumber(), userId);// 分页查询记录
		// 查询一共多少条记录
		int rows = userMapper.selectCommentCount(userId);
		// 计算一共多少页
		int totalPage = rows % page.getPageNumber() == 0 ? rows / page.getPageNumber()
				: rows / page.getPageNumber() + 1;
		// 封装数据到page进行放回
		page.setDate(comment);
		page.setRows(rows);
		page.setTotalPage(totalPage);
		return page;
	}

	@Override
	public List<Comment> selectCommentByfoodId(int foodId) throws RuntimeException {
		// TODO Auto-generated method stub
		return userMapper.selectCommentByfoodId(foodId);
	}

	public User selectUserById(int userId) {
		// TODO Auto-generated method stub
		return userMapper.selectUserById(userId);
	}

	@Override
	public void changeUser(User user) {
		// TODO Auto-generated method stub
		userMapper.changeUser(user);
	}

	@Override
	public int selectCommentCountByfoodId(int foodId) throws RuntimeException {
		// TODO Auto-generated method stub
		return userMapper.selectCommentCountByfoodId(foodId);
	}

	@Override
	public int addComment(Comment comment) {
		// TODO Auto-generated method stub
		comment.setCommentTime(ToolUtil.getCurrentTime());// 设置创建时间
		return userMapper.addComment(comment);
	}

}
